<template>
  <div class="divBox">
    <el-card class="box-card">

      <div slot="header" class="clearfix">

        <el-form inline size="small" @submit.native.prevent>
          <el-form-item label="关键词：">
            <el-input v-model="listPram.businessName" placeholder="输入商家名称" clearable class="selWidth"/>
          </el-form-item>
          <el-button  size="mini" type="primary" @click="getList()">搜索</el-button>
          <el-button  size="mini" type="primary" @click="handleEdit(null)">新增</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>

        </el-form>
      </div>

      <el-table :data="listData.list" size="mini" :header-cell-style="{fontWeight:'bold',background:'#f8f8f9',color: '#515a6e',height:'40px'}" border >
        <el-table-column label="Id" prop="id" min-width="80"/>
        <el-table-column label="商家分成" prop="businessProportion" min-width="130"/>
        <el-table-column label="平台分成" prop="platformProportion" min-width="130"/>
        <el-table-column label="优惠券有效天数" prop="voucherExpirationDay" min-width="130"/>
        <el-table-column label="商家名称" prop="businessNameStr" min-width="150"/>
        <el-table-column label="操作" min-width="180" fixed="right" align="center">
          <template slot-scope="scope">
            <el-button v-if="scope.row.id!==1" icon="el-icon-edit" size="mini" type="primary" @click="handleEdit(scope.row.id)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          :current-page="listPram.page"
          :page-sizes="[20,40,60,80,100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="listData.count"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />
    </el-card>
    <el-dialog
        :visible.sync="editDialog.show"
        :title="editDialog.id ? '编辑' : '新增'"
        destroy-on-close
        :close-on-click-modal="false"
        width="800px">
      <edit
          v-if="editDialog.show"
          :id="editDialog.id"
          :form="editDialog.form"
          @hideEditDialog="hideEditDialog"
          ref="editForm"
      />
    </el-dialog>
    <el-dialog
        :visible.sync="editRecordDialog.show"
        title="记录"
        destroy-on-close
        :close-on-click-modal="false"
        width="800px">
      <record
          v-if="editRecordDialog.show"
          :id="editRecordDialog.id"
          @hideRecordEditDialog="hideRecordEditDialog"
          ref="editForm"
      />
    </el-dialog>
  </div>
</template>

<script>
import edit from './edit.vue'
import record from './record.vue'
import {queryWebConsumptionDivideConfigList} from "@/api/consumptionRules";
import {deleteRow, list, updateStatus} from "@/api/integral";
import {checkPermi} from "@/utils/permission";

export default {
  components: { edit, record },
  data() {
    return {
      listData: { list: [], count: 0 },
      listPram: {
        page: 1,
        limit: 20,
        businessName: null,
      },
      editDialog: {
        show: false,
        id: null,
        form:{}
      },
      editRecordDialog: {
        show: false,
        id: null
      },
      tableLoading: false,
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    handleEdit(id=null) {
      this.editDialog.id = id;
      this.editDialog.show = true;
    },
    resetQuery() {
      this.listPram.businessName = '';
      this.getList()
    },
    checkPermi,
    getList() {
      this.tableLoading = true
      queryWebConsumptionDivideConfigList(this.listPram).then(res => {
        this.listData = res;
      }).finally(() => {
        this.tableLoading = false;
      })
    },
    openRecordEdit(id = null) {
      this.editRecordDialog.id = id;
      this.editRecordDialog.show = true
    },
    handlerDelete(id) {
      this.$confirm('确认删除该数据吗？').then(() => {
        const loading = this.$loading({ text: '删除中', background: 'rgba(255,255,255,.5)' })
        deleteRow(id).then(() => {
          this.$message.success('删除成功')
          this.getList()
        }).finally(() => {
          loading.close()
        })
      })
    },
    hideEditDialog() {
      this.editDialog.show = false
      this.getList()
    },
    hideRecordEditDialog() {
      this.editRecordDialog.show = false
      this.getList()
    },
    handleSizeChange(val) {
      this.listPram.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listPram.page = val
      this.getList()
    },
    handleStatusChange(status, id) {
      const loading = this.$loading({ text: '更新中', background: 'rgba(255,255,255,.5)' })
      updateStatus(id, status).then(() => {
        this.$message.success('更新状态成功')
        this.getList()
      }).finally(() => {
        loading.close()
      })
    },
  }
}
</script>

<style scoped lang="scss">
</style>
